import java.sql.*;

public class DBUtil {
    private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
    private static final String URL = "jdbc:mysql://localhost:3306/db_test";
    private static final String USER = "root";
    private static final String PASS = "123456";
    //获取数据库连接源
    public static Connection getConnection() {
        //引入驱动类库
        try {
            Class.forName(DRIVER);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        try {
            return DriverManager.getConnection(URL,USER,PASS);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    //增删查改-基础版
    //查询功能
    public void select(Connection conn, String sql){
        //Statement 实例化对象:对数据库进行交互
        try {
            Statement statement = conn.createStatement();
            //创建结果集
            ResultSet resultSet = statement.executeQuery(sql);
            while (resultSet.next()) {
                System.out.print(resultSet.getInt(1)+" ");
                System.out.print(resultSet.getString(2)+" ");
                System.out.print(resultSet.getInt(3)+" ");
                System.out.print(resultSet.getString(4)+" ");
                System.out.print(resultSet.getString(5)+" ");
                System.out.print(resultSet.getString(6)+" ");
                System.out.println();
            }
            statement.close();
            resultSet.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    //增加信息功能
    public void insert(Student student,Connection conn){
        String sql = "insert into tb_student(name,age,gender,class,school) values(?,?,?,?,?)";
        //与数据库交互的类
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,student.getName());
            ps.setInt(2,student.getAge());
            ps.setString(3,student.getGender());
            ps.setString(4, student.getClass1());
            ps.setString(5, student.getSchool());
            int num = ps.executeUpdate();
            if(num > 0){
                System.out.println("新增成功");
            }else{
                System.out.println("新增失败");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    //更新（修改）信息功能
    public void update(Student student,Connection conn){
        String sql = "update tb_student set name=?,age=?,gender=?,class=?,school=? where id=?";
        //与数据库交互的类
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,student.getName());
            ps.setInt(2,student.getAge());
            ps.setString(3,student.getGender());
            ps.setString(4, student.getClass1());
            ps.setString(5, student.getSchool());
            ps.setInt(6, student.getId());
            int num = ps.executeUpdate();
            if(num > 0){
                System.out.println("修改成功");
            }else{
                System.out.println("修改失败");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    //删除学生信息功能
    public void delete(int id,Connection conn){
        String sql = "delete from tb_student where id=?";
        //与数据库交互的类
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            int num = ps.executeUpdate();
            if(num > 0){
                System.out.println("删除成功");
            }else{
                System.out.println("删除失败");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

}